package com.burstly.lib.currency;

import android.content.Context;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.Utils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public final class UserAccountStorage
{
  private static final int INITIAL_CACHE_CAPACITY = 4;
  private static final UserAccountStorage INSTANCE = new UserAccountStorage();
  private static final Lock LOCK;
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "CurrencyManager";
  private static File sCacheDir;
  private final Map<String, UserAccount> mAccountCache = new HashMap(4);

  static
  {
    LOCK = new ReentrantLock();
  }

  private static UserAccount createAccount(AccountInfo paramAccountInfo)
  {
    UserAccount localUserAccount = new UserAccount();
    localUserAccount.setPubId(paramAccountInfo.getPubId());
    String str1 = paramAccountInfo.getUserId();
    localUserAccount.setUserId(str1);
    String str2 = Utils.getDeviceId();
    localUserAccount.setDeviceId(str2);
    localUserAccount.setUserIdDeviceIdBased(str2.equals(str1));
    return localUserAccount;
  }

  private static String createFileName(String paramString1, String paramString2)
  {
    String str = paramString1 + paramString2;
    return Utils.sha1(str, String.valueOf(str.hashCode()));
  }

  public static UserAccountStorage getInstance()
  {
    return INSTANCE;
  }

  static void initCacheDirectory(Context paramContext)
  {
    sCacheDir = paramContext.getDir("cm", 0);
  }

  private UserAccount loadAccount(AccountInfo paramAccountInfo)
  {
    String str = createFileName(paramAccountInfo.getUserId(), paramAccountInfo.getPubId());
    try
    {
      LOCK.lock();
      UserAccount localUserAccount = readFile(new File(sCacheDir, str));
      return localUserAccount;
    }
    finally
    {
      LOCK.unlock();
    }
  }

  // ERROR //
  private UserAccount readFile(File paramFile)
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_2
    //   2: new 153	java/io/BufferedInputStream
    //   5: dup
    //   6: new 155	java/io/FileInputStream
    //   9: dup
    //   10: aload_1
    //   11: invokespecial 158	java/io/FileInputStream:<init>	(Ljava/io/File;)V
    //   14: invokespecial 161	java/io/BufferedInputStream:<init>	(Ljava/io/InputStream;)V
    //   17: astore_3
    //   18: new 163	java/io/ObjectInputStream
    //   21: dup
    //   22: aload_3
    //   23: invokespecial 164	java/io/ObjectInputStream:<init>	(Ljava/io/InputStream;)V
    //   26: astore 4
    //   28: aload 4
    //   30: invokevirtual 168	java/io/ObjectInputStream:readObject	()Ljava/lang/Object;
    //   33: astore 11
    //   35: getstatic 32	com/burstly/lib/currency/UserAccountStorage:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   38: ldc 17
    //   40: ldc 170
    //   42: iconst_1
    //   43: anewarray 4	java/lang/Object
    //   46: dup
    //   47: iconst_0
    //   48: aload 11
    //   50: aastore
    //   51: invokevirtual 174	com/burstly/lib/util/LoggerExt:logDebug	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   54: pop
    //   55: aload 11
    //   57: checkcast 54	com/burstly/lib/currency/UserAccount
    //   60: astore 13
    //   62: aload 13
    //   64: aload_1
    //   65: invokevirtual 177	java/io/File:getName	()Ljava/lang/String;
    //   68: invokevirtual 180	com/burstly/lib/currency/UserAccount:setCacheFilename	(Ljava/lang/String;)V
    //   71: aload 4
    //   73: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   76: aload 13
    //   78: areturn
    //   79: astore 19
    //   81: aload_2
    //   82: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   85: aconst_null
    //   86: areturn
    //   87: astore 18
    //   89: getstatic 32	com/burstly/lib/currency/UserAccountStorage:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   92: ldc 17
    //   94: ldc 186
    //   96: iconst_1
    //   97: anewarray 4	java/lang/Object
    //   100: dup
    //   101: iconst_0
    //   102: aload_1
    //   103: aastore
    //   104: invokevirtual 189	com/burstly/lib/util/LoggerExt:logWarning	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   107: pop
    //   108: aload_2
    //   109: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   112: goto -27 -> 85
    //   115: astore 17
    //   117: getstatic 32	com/burstly/lib/currency/UserAccountStorage:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   120: ldc 17
    //   122: ldc 191
    //   124: iconst_0
    //   125: anewarray 4	java/lang/Object
    //   128: invokevirtual 189	com/burstly/lib/util/LoggerExt:logWarning	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   131: pop
    //   132: aload_2
    //   133: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   136: goto -51 -> 85
    //   139: astore 7
    //   141: aload_2
    //   142: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   145: aload 7
    //   147: athrow
    //   148: astore 7
    //   150: aconst_null
    //   151: astore_2
    //   152: goto -11 -> 141
    //   155: astore 7
    //   157: aload 4
    //   159: astore_2
    //   160: goto -19 -> 141
    //   163: astore 16
    //   165: aconst_null
    //   166: astore_2
    //   167: goto -50 -> 117
    //   170: astore 9
    //   172: aload 4
    //   174: astore_2
    //   175: goto -58 -> 117
    //   178: astore 15
    //   180: aconst_null
    //   181: astore_2
    //   182: goto -93 -> 89
    //   185: astore 6
    //   187: aload 4
    //   189: astore_2
    //   190: goto -101 -> 89
    //   193: astore 14
    //   195: aconst_null
    //   196: astore_2
    //   197: goto -116 -> 81
    //   200: astore 5
    //   202: aload 4
    //   204: astore_2
    //   205: goto -124 -> 81
    //
    // Exception table:
    //   from	to	target	type
    //   2	18	79	java/io/FileNotFoundException
    //   2	18	87	java/io/IOException
    //   2	18	115	java/lang/ClassNotFoundException
    //   2	18	139	finally
    //   89	108	139	finally
    //   117	132	139	finally
    //   18	28	148	finally
    //   28	71	155	finally
    //   18	28	163	java/lang/ClassNotFoundException
    //   28	71	170	java/lang/ClassNotFoundException
    //   18	28	178	java/io/IOException
    //   28	71	185	java/io/IOException
    //   18	28	193	java/io/FileNotFoundException
    //   28	71	200	java/io/FileNotFoundException
  }

  public void clearStorage(String paramString)
  {
    try
    {
      LOCK.lock();
      File localFile = new File(sCacheDir, paramString);
      if ((!localFile.exists()) || (!localFile.delete()))
      {
        LoggerExt localLoggerExt = LOG;
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = localFile.getName();
        localLoggerExt.logWarning("CurrencyManager", "Failed to delete user account data file {0}", arrayOfObject);
      }
      return;
    }
    finally
    {
      LOCK.unlock();
    }
  }

  UserAccount getAccount(AccountInfo paramAccountInfo, boolean paramBoolean)
  {
    String str1 = paramAccountInfo.getUserId();
    String str2 = paramAccountInfo.getPubId();
    String str3 = str1 + str2;
    UserAccount localUserAccount = (UserAccount)this.mAccountCache.get(str3);
    if (localUserAccount == null)
    {
      localUserAccount = loadAccount(paramAccountInfo);
      if ((paramBoolean) && (localUserAccount == null))
        localUserAccount = createAccount(paramAccountInfo);
      this.mAccountCache.put(str3, localUserAccount);
    }
    return localUserAccount;
  }

  List<UserAccount> getAllAccounts()
  {
    ArrayList localArrayList = new ArrayList();
    LOCK.lock();
    File[] arrayOfFile = sCacheDir.listFiles();
    int i = arrayOfFile.length;
    for (int j = 0; j < i; j++)
    {
      UserAccount localUserAccount = readFile(arrayOfFile[j]);
      if (localUserAccount != null)
        localArrayList.add(localUserAccount);
    }
    LOCK.unlock();
    return localArrayList;
  }

  String[] getCachedFileList()
  {
    try
    {
      LOCK.lock();
      String[] arrayOfString = sCacheDir.list();
      return arrayOfString;
    }
    finally
    {
      LOCK.unlock();
    }
  }

  boolean hasCachedFiles(AccountInfo paramAccountInfo)
  {
    String str = createFileName(paramAccountInfo.getUserId(), paramAccountInfo.getPubId());
    try
    {
      LOCK.lock();
      boolean bool = new File(sCacheDir, str).exists();
      return bool;
    }
    finally
    {
      LOCK.unlock();
    }
  }

  // ERROR //
  public void saveAccount(UserAccount paramUserAccount)
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_2
    //   2: getstatic 39	com/burstly/lib/currency/UserAccountStorage:LOCK	Ljava/util/concurrent/locks/Lock;
    //   5: invokeinterface 133 1 0
    //   10: aload_1
    //   11: invokevirtual 243	com/burstly/lib/currency/UserAccount:getCacheFilename	()Ljava/lang/String;
    //   14: astore 6
    //   16: aconst_null
    //   17: astore_2
    //   18: aload 6
    //   20: ifnonnull +22 -> 42
    //   23: aload_1
    //   24: invokevirtual 244	com/burstly/lib/currency/UserAccount:getUserId	()Ljava/lang/String;
    //   27: aload_1
    //   28: invokevirtual 245	com/burstly/lib/currency/UserAccount:getPubId	()Ljava/lang/String;
    //   31: invokestatic 128	com/burstly/lib/currency/UserAccountStorage:createFileName	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   34: astore 6
    //   36: aload_1
    //   37: aload 6
    //   39: invokevirtual 180	com/burstly/lib/currency/UserAccount:setCacheFilename	(Ljava/lang/String;)V
    //   42: new 247	java/io/ObjectOutputStream
    //   45: dup
    //   46: new 249	java/io/BufferedOutputStream
    //   49: dup
    //   50: new 251	java/io/FileOutputStream
    //   53: dup
    //   54: new 135	java/io/File
    //   57: dup
    //   58: getstatic 125	com/burstly/lib/currency/UserAccountStorage:sCacheDir	Ljava/io/File;
    //   61: aload 6
    //   63: invokespecial 138	java/io/File:<init>	(Ljava/io/File;Ljava/lang/String;)V
    //   66: invokespecial 252	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   69: invokespecial 255	java/io/BufferedOutputStream:<init>	(Ljava/io/OutputStream;)V
    //   72: invokespecial 256	java/io/ObjectOutputStream:<init>	(Ljava/io/OutputStream;)V
    //   75: astore 7
    //   77: aload 7
    //   79: aload_1
    //   80: invokevirtual 260	java/io/ObjectOutputStream:writeObject	(Ljava/lang/Object;)V
    //   83: getstatic 32	com/burstly/lib/currency/UserAccountStorage:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   86: ldc 17
    //   88: ldc_w 262
    //   91: iconst_1
    //   92: anewarray 4	java/lang/Object
    //   95: dup
    //   96: iconst_0
    //   97: aload_1
    //   98: aastore
    //   99: invokevirtual 174	com/burstly/lib/util/LoggerExt:logDebug	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   102: pop
    //   103: aload 7
    //   105: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   108: getstatic 39	com/burstly/lib/currency/UserAccountStorage:LOCK	Ljava/util/concurrent/locks/Lock;
    //   111: invokeinterface 145 1 0
    //   116: return
    //   117: astore 4
    //   119: getstatic 32	com/burstly/lib/currency/UserAccountStorage:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   122: ldc 17
    //   124: ldc_w 264
    //   127: iconst_0
    //   128: anewarray 4	java/lang/Object
    //   131: invokevirtual 267	com/burstly/lib/util/LoggerExt:logError	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   134: pop
    //   135: aload_2
    //   136: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   139: getstatic 39	com/burstly/lib/currency/UserAccountStorage:LOCK	Ljava/util/concurrent/locks/Lock;
    //   142: invokeinterface 145 1 0
    //   147: return
    //   148: astore_3
    //   149: aload_2
    //   150: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   153: getstatic 39	com/burstly/lib/currency/UserAccountStorage:LOCK	Ljava/util/concurrent/locks/Lock;
    //   156: invokeinterface 145 1 0
    //   161: aload_3
    //   162: athrow
    //   163: astore_3
    //   164: aload 7
    //   166: astore_2
    //   167: goto -18 -> 149
    //   170: astore 8
    //   172: aload 7
    //   174: astore_2
    //   175: goto -56 -> 119
    //
    // Exception table:
    //   from	to	target	type
    //   2	16	117	java/io/IOException
    //   23	42	117	java/io/IOException
    //   42	77	117	java/io/IOException
    //   2	16	148	finally
    //   23	42	148	finally
    //   42	77	148	finally
    //   119	135	148	finally
    //   77	103	163	finally
    //   77	103	170	java/io/IOException
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.currency.UserAccountStorage
 * JD-Core Version:    0.6.2
 */